package com.ymk.ctrl;


import com.google.gson.Gson;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.cmd.SchedulerCmd;
import com.ymk.base.exception.MallException;
import com.ymk.base.service.OssService;
import com.ymk.base.utlis.LogUtils;
import com.ymk.core.code.resp.RespCode;
import com.ymk.core.constants.SysConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
public class HttpHandlerCtrl {
    private final static Logger logger = LoggerFactory.getLogger(HttpHandlerCtrl.class);


    @RequestMapping(path = "/oss/api", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    String ssoApi(@RequestParam("file") MultipartFile file) {
        try {
            return this.ossService.ossUpload(file);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "";
    }

    @RequestMapping(path = "/server/api", method = RequestMethod.POST)
    @ResponseBody
    String serverApi(@RequestBody String body, HttpServletRequest request) {
        try {
            CmdSign cmdSign = new Gson().fromJson(body, CmdSign.class);
            cmdSign.setScope(SysConstants.CMD_SERVER_SCOPE);
            return this.execute(cmdSign, request).toString();
        } catch (Exception e) {
            logger.error(body);
            logger.error(e.getMessage(), e);
        }
        return "";
    }

    @RequestMapping(path = "/sop/api", method = RequestMethod.POST)
    @ResponseBody
    String supApi(@RequestBody String body, HttpServletRequest request) {
        try {
            CmdSign cmdSign = new Gson().fromJson(body, CmdSign.class);
            cmdSign.setScope(SysConstants.CMD_SOP_SCOPE);
            return this.execute(cmdSign, request).toString();
        } catch (Exception e) {
            logger.error(body);
            logger.error(e.getMessage(), e);
        }
        return "";
    }

    @RequestMapping(path = "/mop/api", method = RequestMethod.POST)
    @ResponseBody
    String mopApi(@RequestBody String body, HttpServletRequest request) {
        try {
            CmdSign cmdSign = new Gson().fromJson(body, CmdSign.class);
            cmdSign.setScope(SysConstants.CMD_MOP_SCOPE);
            return this.execute(cmdSign, request).toString();
        } catch (Exception e) {
            logger.error(body);
            logger.error(e.getMessage(), e);
        }
        return "";
    }


    @RequestMapping(path = "/store/api", method = RequestMethod.POST)
    @ResponseBody
    String storeApi(@RequestBody String body, HttpServletRequest request) {
        try {
            CmdSign cmdSign = new Gson().fromJson(body, CmdSign.class);
            cmdSign.setScope(SysConstants.CMD_STORE_SCOPE);
            return this.execute(cmdSign, request).toString();
        } catch (Exception e) {
            logger.error(body);
            logger.error(e.getMessage(), e);
        }
        return "";
    }

    private CmdSign execute(CmdSign cmdSign, HttpServletRequest request) {
        CmdSign respSign = null;
        try {
            respSign = this.scheduler.schedule(request, cmdSign);
        } catch (MallException e) {
            cmdSign.endExe();
            respSign = new CmdSign(cmdSign);
            respSign.setRespCode(e.getRespCode());
            respSign.setMsg(e.getMsg());
            LogUtils.auditLog(logger, cmdSign, "", respSign.getMsg());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            if (cmdSign != null) {
                logger.error(new Gson().toJson(cmdSign));
                cmdSign.endExe();
                respSign = new CmdSign(cmdSign);
                respSign.setRespCode(RespCode.CODE_ERROR);
                respSign.setMsg("异常操作，请联系服务商");
            }
        }
        // 效率日志
        // LogUtils.statLog(logger, cmdSign, LogUtils.LOG_GROUP_STAT_CMD);
        if (respSign == null) {
            cmdSign.setMsg("execute return null");
            logger.error(new Gson().toJson(cmdSign));
        }
        return respSign;
    }



    @Autowired
    private SchedulerCmd scheduler;
    @Autowired
    private OssService ossService;
}
